package org.edu.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
/**
 * <h2>部门表</h2>
 * <p>该实例 主要配置 用户与部门many-to-one
 * Emp 为one 方
 * <br>并写明了配置要点
 * </p> 
 * @author 谢圣强
 * @version 1.1
 * @see User
 */
@Entity
@Table(name = "T_emp")
public class Emp {
	private int empno;// 部门编号
	private String empname;// 部门名称
	private int pno;
	// private boolean isparent;

	private Set<User> userid;

	@Id
	@GenericGenerator(name = "idGenerator", strategy = "sequence", parameters = { @Parameter(name = "sequence", value = "seq_empid") })
	@GeneratedValue(generator = "idGenerator")
	public int getEmpno() {
		return empno;
	}

	public void setEmpno(int empno) {
		this.empno = empno;
	}

	@Column(name = "tb_empname")
	public String getEmpname() {
		return empname;
	}

	public void setEmpname(String emoname) {
		this.empname = emoname;
	}

	@Column(name = "tb_pno")
	public int getPno() {
		return pno;
	}

	public void setPno(int pno) {
		this.pno = pno;
	}

	// @Column(name = "tb_parent")
	// public boolean isIsparent() {
	// return isparent;
	// }
	//
	// public void setIsparent(boolean isparent) {
	// this.isparent = isparent;
	// }
	@OneToMany(targetEntity = User.class, cascade = { CascadeType.REFRESH,
			CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE }, fetch = FetchType.LAZY)
	@Fetch(FetchMode.SELECT)
	// updatable=false很关键，如果没有它，在级联删除的时候就会报错(反转的问题)
	@JoinColumn(name = "empid", updatable = false)
	public Set<User> getUserid() {
		return userid;
	}

	public void setUserid(Set<User> userid) {
		this.userid = userid;
	}

	@Override
	public String toString() {
		// ,\'isParent\':"+isparent+"
		return "{\'id\':" + empno + ",\'pId\':" + pno + ",\'name\':\'"
				+ empname + "\',\'icon\':\'../dist/css/zTreeStyle/img/diy/3.png\'}";
	}
	//,\'isParent\':\'true\'

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if(null!=obj) 
			if(obj instanceof Emp){
				Emp e=(Emp)obj;
				if(this.empno==e.getEmpno()&&this.pno==e.pno&&this.empname.equals(e.getEmpname()))
					return true;
		}
		return false;
	}

	
	
}
